<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/logo.png" width="50px" height="50px"/>
        <span>后台管理</span>
      </div>
      <el-button type="info" @click="logout()">退出</el-button>
    </el-header>
    <el-container>
      <!-- 左侧菜单 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">《》</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <el-submenu :index="item.id +''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+itemChildren.path +''" v-for="itemChildren in item.children" :key="itemChildren.id" @click="saveNavState('/'+itemChildren.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{itemChildren.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
/* eslint-disable */
  export default {
    data(){
      return{
        //左侧菜单数据
        menulist:[],
        iconsObj:{
          125:'el-icon-s-custom',
          103:'el-icon-s-tools',
          101:'el-icon-s-shop',
          102:'el-icon-s-order',
          145:'el-icon-s-data'
        },
        isCollapse:false,
        activePath:''
      }
    },
    created(){
      this.getMenuList()
      this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods:{
      logout(){
        window.sessionStorage.clear()
        this.$router.push('/login')
      },
      // 获取菜单列表
      getMenuList(){
        this.$http.get('menus').then(res => {
          if(res.data.meta.status != 200) return this.$message.error(res.data.meta.msg)
          this.menulist = res.data.data
        })
      },
      //关闭展开菜单
      toggleCollapse(){
        this.isCollapse = !this.isCollapse
      },
      //保持菜单高量显示
      saveNavState(activePath){
        window.sessionStorage.setItem('activePath',activePath)
        this.activePath = activePath
      }
    }
  }
</script>

<style scoped lang="less">
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    font-size: 20px;
    padding-left: 10px;
    color:#fff;
    align-items: center;
    > div {
      display: flex;
      align-items: center;
      span {
        margin-left: 10px;
      }
    }
  }

  .el-aside {
    background-color: #333744;
    .el-menu {
      border-right: none;
    }
  }

  .el-main {
    background-color: #eaedf1;
  }
  .home-container {
    height: 100%;
  }
  .toggle-button {
    background-color: #505458;
    color:#fff;
    text-align: center;
    cursor: pointer;
  }
</style>
